package com.ddyx.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ddyx.entity.Goods;
import com.ddyx.entity.GoodsImage;
import com.ddyx.mapper.GoodsImageMapper;
import com.ddyx.mapper.GoodsMapper;
import com.ddyx.service.IStoreGoodsService;
import com.ddyx.util.MyBeanUtils;
import com.ddyx.util.Page;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;
import com.ddyx.vo.GoodsDetails;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class StoreGoodsServiceImpl implements IStoreGoodsService{

	@Resource
	GoodsMapper goodsMapper;
	@Resource 
	GoodsImageMapper goodsImageMapper;
	
	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm getStoreGoods(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String,Object> paramMap = (Map<String,Object>)param.getData();
		try {
			List<Map<String,Object>> goodsList = goodsMapper.getStoreGoods(Page.parse(paramMap));
			result.setData(goodsList);
			int total = goodsMapper.countStoreGoods(paramMap);
			result.setTotal(total);
		} catch (Exception e) {
			result.setStatus(false);
			result.setMessage("查询商品列表错误");
			log.error(e+"查询商品列表错误");
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm getGoodsDetails(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String,Object> requestMap = (Map<String,Object>)param.getData();
		Goods goodsQuery = new Goods();
		GoodsImage goodsImageQuery = new GoodsImage();
		try {
			List<Map<String,Object>> goodsList = goodsMapper.getStoreGoods(Page.parse(requestMap));
			result.setTotal(1);
			Map<String,Object> goodsMap = new HashMap<String,Object>();
			if(goodsList!=null)
			{
				goodsMap = goodsList.get(0);
			}
			GoodsDetails goodsDetails = new GoodsDetails();
			MyBeanUtils.populate(goodsDetails, goodsMap);
//			Goods goods = goodsMapper.selectByPrimaryKey(goodsQuery);
			String discountPrice=goodsDetails.getDiscountPrice();
			if(null==discountPrice || 0==discountPrice.length() || "null".equals(discountPrice) || "0".equals(discountPrice) 
				|| "0.00".equals(discountPrice) || "0.0".equals(discountPrice)) {
				goodsDetails.setDiscountPrice("");
			}
			/*BigDecimal goodsPrice=goodsDetails.getGoodsPrice();
			if(null==discountPrice || 0==discountPrice.length() || "null".equals(discountPrice) || "0".equals(discountPrice) 
					|| "0.00".equals(discountPrice) || "0.0".equals(discountPrice)) {
				goodsDetails.setGoodsPrice(new BigDecimal(""));
			}*/
			MyBeanUtils.populate(goodsImageQuery, requestMap);
			List<GoodsImage> goodsPics = goodsImageMapper.select(goodsImageQuery);
			if(goodsPics!=null)
				goodsDetails.setGoodsPics(goodsPics);
			
			result.setData(goodsDetails);
		} catch (Exception e) {
			result.setStatus(false);
			result.setMessage("查询商品详情错误");
			log.error(e+"查询商品详情错误");
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm insertwxGood(RequestForm param) {
		ResponseForm result = new ResponseForm();
//		Map<String,Object> paramMap = (Map<String,Object>)param.getData();
//		try {
//			paramMap.put("createTime", DateUtil.format_ymd_hms.format(new Date()));
//			int index = goodMapper.insertwxGood(paramMap);
//			if(index < 0) {
//				result.setStatus(false);
//				result.setMessage("加入商品失败");
//			}
//		} catch (Exception e) {
//			result.setStatus(false);
//			result.setMessage("查询商品详情错误");
//			log.error(e+"查询商品详情错误");
//		}
		return result;
	}

}
